package Bai04;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

public class Threading extends Thread{
	Socket con;
	public Threading(Socket c){
		con=c;
		start();
	}
	public void run(){
		InputStream is;
		OutputStream os;
		BufferedReader br;
		PrintStream fs;
		String str="";
		
		try {
			is=con.getInputStream();
			os=con.getOutputStream();
			br=new BufferedReader(new InputStreamReader(is));
			fs=new PrintStream(os);
			while(true){
				str=br.readLine();
				String cmd=str.substring(0, str.indexOf("("));
				String text=str.substring(str.indexOf("(")+1,str.length()-1);
				switch (cmd) {
				case "UPPER":
					text=text.toUpperCase();
					break;
				case "LOWER":
					text=text.toLowerCase();
					break;
				case "INSERT":
					String[] sq=text.split(",");
					int k=Integer.parseInt(sq[2]);
					String temp=sq[0].substring(k,sq[0].length());
					sq[0]=sq[0].substring(0,k);
					sq[0]=sq[0].concat(sq[1]);
					sq[0]=sq[0].concat(temp);
					text=new String();
					text=sq[0];
					break;
				case "DELETE":
					String[] s=text.split(",");
					int i=Integer.parseInt(s[1]);
					int n=Integer.parseInt(s[2]);
					String t=s[0].substring(i+n,s[0].length());
					s[0]=s[0].substring(0,i);
					s[0]=s[0].concat(t);
					text=new String();
					text=s[0];
					break;
					
				default:
					break;
				}
				fs.println(text);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
